package com.nondo.dean.auth.controller.api.inner;

import com.nondo.dean.auth.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.nondo.dean.auth.domain.User;
import com.nondo.dean.auth.module.UserBean;
import com.nondo.dean.auth.service.UserService;
import com.nondo.dean.auth.service.impl.SapUserServiceImpl;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.View;

/**
 * @author xiaofan
 * @Description: 获取sap返回的用户信息
 * @throws
 * @date 2018-05-1115:33
 * @Version 0.1
 */
@RestController("apiInnerAuthUser")
@RequestMapping("/{v}/inner/auth/user")
public class UserController {

    @Autowired
    SapUserServiceImpl sapUserService;
    
    @Autowired
    UserService userService;
    

    /**
     * 接收sap返回的客户数据入库
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public int synUser(@RequestBody String result){
        Object jsonObject= JSONObject.parse(result);
        String jsonString=jsonObject.toString();
        return sapUserService.insertUser(JSONObject.parseArray(jsonString, UserBean.class));

    }
    
    /**
     * 按照ID查询用户
     * @param userId
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{userId}")
    public View<User> getUserById(@PathVariable("userId") Long userId) {
        View<User> view = new View<User>(20000, "获取数据成功");
        User user = userService.findByUserId(userId);
        view.setData(user);
        return view;

    }
    
    /**
     * 按照username获取用户
     * @param userId
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/get/{username}")
    public View<User> getUserById(@PathVariable("username") String username) {
        View<User> view = new View<User>(20000, "获取数据成功");
        User user = userService.findByUsername(username);
        view.setData(user);
        return view;

    }

    /**
     * 查询接单员对应的sap账号
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/search/sap/account/{id}")
    public View<String> querySapAccount(@PathVariable("id") Long id){
        View<String> view = new View<String>(20000,"获取数据成功");
        view.setData(userService.getSapAccount(id));
        return view;
    }
}
